package com.wangziyu.facerecognitiongate

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.wangziyu.facerecognitiongate.adapter.TreeAdapter
import com.wangziyu.facerecognitiongate.adapter.TreeItemDecoration
import com.wangziyu.facerecognitiongate.bean.TreeNode
import com.wangziyu.facerecognitiongate.databinding.ActivityMainBinding
import com.wangziyu.facerecognitiongate.printer.Demo

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        Demo.demo()

        // 构造数据
        val childA1 = TreeNode(2, "子节点 A1")
        val childA2 = TreeNode(2, "子节点 A1")

        val childB1 = TreeNode(3, "子节点 B1")

        val childC1 = TreeNode(4, "子节点 C1")
        val childC2 = TreeNode(4, "子节点 C2")
        val childC3 = TreeNode(4, "子节点 C3")

        val childA = TreeNode(2, "子节点 A", mutableListOf(childA1, childA2))
        val childB = TreeNode(3, "子节点 B", mutableListOf(childB1))
        val childC = TreeNode(4, "子节点 C", mutableListOf(childC1, childC2, childC3))
        val root = TreeNode(1, "根节点", mutableListOf(childA, childB, childC))

        setNodeLevels(root)
        setNodeLevels(childA,1)
        setNodeLevels(childB,1)
        setNodeLevels(childC,1)

        val adapter = TreeAdapter(listOf(root))
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.addItemDecoration(TreeItemDecoration(Color.GRAY, 3f, adapter))
    }

    // 递归设置每个节点的 level 和 parent
    private fun setNodeLevels(node: TreeNode, level: Int = 0) {
        node.level = level
        node.children.forEach {
            it.parent = node
            setNodeLevels(it, level + 1)
        }
    }
}
